package com.qf.wangqing.sportsnews.adapter.headline;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.percent.PercentFrameLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.qf.wangqing.sportsnews.R;
import com.qf.wangqing.sportsnews.bean.HeadEntity.DanymicEntitylists;
import com.squareup.picasso.Picasso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by ShenHao on 2016/12/19.
 */

public class AdapterDanymicRec extends RecyclerView.Adapter<AdapterDanymicRec.ViewHolder> implements MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener, View.OnClickListener {


    private List<DanymicEntitylists> list;
    private Context context;
    private MediaPlayer player;
    private ViewHolder holder;


    public AdapterDanymicRec(Context context) {
        this.list = new ArrayList<>();
        this.context = context;
        player = new MediaPlayer();
        player.setOnCompletionListener(this);


    }

    @Override
    public int getItemViewType(int position) {
        if (list.get(position).getImages().size() == 1) {
            return R.layout.danymic_item_video;
        } else {
            return R.layout.danymic_ityem_picture;
        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.danymic_item_video, parent, false);
        ViewHolder holder = new ViewHolder(view);
        switch (viewType) {
            case R.layout.danymic_item_video:
                holder.gridView.setVisibility(View.GONE);
                holder.container.setVisibility(View.VISIBLE);
                break;
            case R.layout.danymic_ityem_picture:
                holder.gridView.setVisibility(View.VISIBLE);
                holder.container.setVisibility(View.GONE);
                break;
        }
        if (holder.surfaceView != null) {
            holder.surfaceView.setOnClickListener(this);
            holder.surfaceView.setTag(holder);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.nickName.setText(list.get(position).getSource_data_obj().getNick_name());
        holder.up_time.setText(list.get(position).getUpdate_time());
        holder.video_from.setText(list.get(position).getSource_data_obj().getUrl_type() == 0 ? "来自微博" : "来自秒拍");
        holder.dataText.setText(list.get(position).getText());


        Picasso.with(context).load(list.get(position).getSource_data_obj().getAvatar()).into(holder.user_image);
        Picasso.with(context).load(list.get(position).getImage()).into(holder.image_first);

        if (list.get(position).getImages().size() > 1 && holder.gridView != null) {
            holder.gridView.setAdapter(new AdapterGridView(list.get(position).getImages(), context));
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void add(DanymicEntitylists d) {
        add(list.size(), d);
    }

    public void add(int position, DanymicEntitylists d) {
        list.add(position, d);
        notifyItemInserted(position);
    }

    public void addAll(Collection<? extends DanymicEntitylists> collection) {
        addAll(collection, list.size());
    }

    public void addAll(Collection<? extends DanymicEntitylists> collection, int position) {
        list.addAll(position, collection);
        notifyItemRangeInserted(position, list.size());

    }

    public void remove(DanymicEntitylists d) {
        int index = list.indexOf(d);
        remove(index);
    }

    public void remove(int position) {
        list.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        holder.image_play.setVisibility(View.VISIBLE);
        holder.image_first.setVisibility(View.VISIBLE);
        holder.video_progress.setVisibility(View.INVISIBLE);
        holder = null;
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        player.start();
        holder.image_first.setVisibility(View.INVISIBLE);
        holder.video_progress.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        if (this.holder == holder) {
            player.stop();
            holder.image_play.setVisibility(View.VISIBLE);
            holder.image_first.setVisibility(View.VISIBLE);
            holder.video_progress.setVisibility(View.VISIBLE);
            this.holder = null;
        }
    }

    @Override
    public void onClick(View view) {
        if (holder == null || holder != view.getTag()) {
            if (holder != null) {
                holder.video_progress.setVisibility(View.INVISIBLE);
                holder.image_first.setVisibility(View.VISIBLE);
                holder.image_play.setVisibility(View.VISIBLE);
            }
            holder = (ViewHolder) view.getTag();
            holder.image_play.setVisibility(View.INVISIBLE);
            holder.video_progress.setVisibility(View.VISIBLE);
            player.setDisplay(holder.surfaceView.getHolder());
            player.reset();
            int position = holder.getAdapterPosition();
            try {
                player.setDataSource(list.get(position).getVideo_link());
                player.prepareAsync();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            if (player.isPlaying()) {
                player.pause();
                holder.image_play.setVisibility(View.VISIBLE);
            } else {
                player.start();
                holder.image_play.setVisibility(View.INVISIBLE);
            }
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView user_image, image_first, image_play;
        private TextView nickName, up_time, video_from, dataText;
        private SurfaceView surfaceView;
        private RecyclerView gridView;
        private ProgressBar video_progress;
        private PercentFrameLayout container;

        public ViewHolder(View view) {
            super(view);
            user_image = (ImageView) view.findViewById(R.id.user_icon);
            image_first = (ImageView) view.findViewById(R.id.image_first);
            image_play = (ImageView) view.findViewById(R.id.image_play);

            nickName = (TextView) view.findViewById(R.id.nickName);
            up_time = (TextView) view.findViewById(R.id.up_time);
            video_from = (TextView) view.findViewById(R.id.video_from);
            dataText = (TextView) view.findViewById(R.id.dataText);

            surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);

            video_progress = (ProgressBar) view.findViewById(R.id.video_progress);

            gridView = (RecyclerView) view.findViewById(R.id.gridView);

            container = (PercentFrameLayout) view.findViewById(R.id.container);


        }
    }
}
